#version 130

uniform sampler2D tex;

in vec2 fragmentTexCoord;

out vec4 fragColor;

const int matrixSize = 8;

const float ditheringMatrix[64] = float[](
     0, 32,  8, 40,  2, 34, 10, 42,
    48, 16, 56, 24, 50, 18, 58, 26, 
    12, 44,  4, 36, 14, 46,  6, 38,
    60, 28, 52, 20, 62, 30, 54, 22,
     3, 35, 11, 43,  1, 33,  9, 41,
    51, 19, 59, 27, 49, 17, 57, 25,
    15, 47,  7, 39, 13, 45,  5, 37,
    63, 31, 55, 23, 61, 29, 53, 21);

void main(void){
	//vec4 I = vec4(0.299, 0.587, 0.114, 0.0);
	//float color = dot(texture(tex, fragmentTexCoord), I);
	vec4 color = texture(tex, fragmentTexCoord);

	ivec2 coord = ivec2(mod(fragmentTexCoord * textureSize(tex,0), matrixSize));

	int x = coord.x + matrixSize * coord.y;
	// 1/64 = 0.015625
	float threshold = ditheringMatrix[x]*0.015625;

	color = step(threshold, color); 

	fragColor = color;
}
